﻿
using System;
using System.Data;
using System.Text;
using System.Windows.Forms;
using YNCNumberSys.db;
using myDBConnection = System.Data.SqlClient.SqlConnection;
using myDBTransaction = System.Data.SqlClient.SqlTransaction;
using cadApp = Autodesk.AutoCAD.ApplicationServices.Application;

namespace YNCNumberSys
{
    /// <summary>
    /// XSL 改了2处，正常不改，应该也不出问题。20221121
    /// </summary>
    public partial class UsmInitUser : Form
    {
        CommandManager commandManager = new CommandManager();
        ConnectionManager connectionManager = new ConnectionManager();
        DataTable m_用户 = new DataTable();
        public UsmInitUser()
        {
            InitializeComponent();
            InitFlexGrid();   
        }
        private void InitFlexGrid()
        {
            m_flex.Clear();
            m_flex.DataSource = null;
            m_flex.SelectionMode = C1.Win.C1FlexGrid.SelectionModeEnum.ListBox;
            m_flex.Rows.Count = 1;
            m_flex.Cols.Count = 1;
            userDal dal = new userDal();
            DataTable dt=dal.GetAllUser();
            m_flex.DataSource = dt;
            for (int r = 1; r < m_flex.Rows.Count; r++)
            {
                m_flex[r, 0] = r;
            }
            if (m_flex.Cols.Contains("ID"))
                m_flex.Cols["ID"].Visible = false;
        }
        private void btn_删除用户_Click(object sender, EventArgs e)
        {
            int cur = m_flex.RowSel;
            if(cur>-1)
            {
                string usercode= (string)m_flex.Rows[cur]["用户名"];
                string username= (string)m_flex.Rows[cur]["姓名"];
                if (usercode!="admin")
                {
                    if (!string.IsNullOrEmpty(usercode))
                    {
                        userDal dal = new userDal();
                        if (dal.DelUser(usercode) == 1)
                        {
                            string result = string.Format("用户{0}  {1}已经删除", usercode, username);
                            cadApp.ShowAlertDialog(result);
                            InitFlexGrid();
                        }
                    }
                }
                else
                {
                    cadApp.ShowAlertDialog("admin用户不允许删除！");
                }
            }
            else
            {
                cadApp.ShowAlertDialog("先选中对象用户所在的行！");
            }
        }
        private void btn_密码恢复_Click(object sender, EventArgs e)
        {
            int cur = m_flex.RowSel;
            if (cur > -1)
            {
                int id= (int)m_flex.Rows[cur]["ID"];
                string usercode = (string)m_flex.Rows[cur]["用户名"];
                if (!string.IsNullOrEmpty(usercode))
                {
                    userDal dal = new userDal();
                    if (dal.UpdatePwd(id, EnOrDeEncryptHelper.EncryptText("123")) == 1)
                    {
                        InitFlexGrid();
                    }
                }
            }
            else
            {
                cadApp.ShowAlertDialog("先选中对象用户所在的行！");
            }
        }
        /// <summary>
        /// 刷新用户列表。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonRefreshUserList_Click(object sender, EventArgs e)
        {
            InitFlexGrid();
        }
        /// <summary>
        /// 新建用户。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_新建用户_Click(object sender, EventArgs e)
        {
            UsmNewUser form = new UsmNewUser();
            form.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            form.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            form.MaximizeBox = false;
            form.MinimizeBox = false;
            form.ResumeLayout(false);
            form.DialogResult = DialogResult.None;
            cadApp.ShowModalDialog(cadApp.MainWindow.Handle, form, false);
            InitFlexGrid();
        }

        private void InitUser_Load(object sender, EventArgs e)
        {

        }


    }
}
